#include "postgres.h"
#include "executor/spi.h" /* this is what you need to work with SPI */
#include "commands/trigger.h" /* ... and triggers */
#include <stdlib.h>
#include <stdio.h>
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

extern Datum trigf(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(trigf);
Datum
trigf(PG_FUNCTION_ARGS)
{
	TriggerData *trigdata = (TriggerData *) fcinfo->context;
	TupleDesc tupdesc;
	HeapTuple rettuple;
	char *when;
        
	char* psn;
	char* psm;
	char* prn;
	char* prm;
	char* pstate;
	char* pnum;
	char* ptaskno;
	char* pcompanyid;
	char* pcompanyname;

	bool checknull = false;
	bool isnull;
	int ret, i;
	char pszcmd[256];
	memset(pszcmd,0,256);

        elog(INFO,"this is from harold");
	/* make sure it’s called as a trigger at all */
	if (!CALLED_AS_TRIGGER(fcinfo))
		elog(ERROR, "trigf: not called by trigger manager");
	/* tuple to return to executor */
	if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
		rettuple = trigdata->tg_newtuple;
	else
		rettuple = trigdata->tg_trigtuple;
	/* check for null values */
	if (!TRIGGER_FIRED_BY_DELETE(trigdata->tg_event)
	&& TRIGGER_FIRED_BEFORE(trigdata->tg_event))
		checknull = true;
	if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
		when = "before";
	else
		when = "after ";
	tupdesc = trigdata->tg_relation->rd_att;


        psn = SPI_getvalue(rettuple,tupdesc,2);
        elog(INFO,"the send name is %s",psn);

        psm = SPI_getvalue(rettuple,tupdesc,3);
	elog(INFO,"the send mobile is %s",psm);


        prn= SPI_getvalue(rettuple,tupdesc,7);
        elog(INFO,"the receive mobile is %s",prn);


        prm = SPI_getvalue(rettuple,tupdesc,8);
        elog(INFO,"the receive name is %s",prm);
        //pfree(pName);


	pstate=SPI_getvalue(rettuple,tupdesc,13);
	elog(INFO,"the state is %s",pstate);

	if(strcmp(pstate,"2") == 0)
		elog(INFO,"your state is 2");
	if(strcmp(pstate,"4") == 0)
		elog(INFO,"your state is 4");

        pnum=SPI_getvalue(rettuple,tupdesc,26);
        elog(INFO,"the article num is is %s",pnum);

	ptaskno=SPI_getvalue(rettuple,tupdesc,17);
        elog(INFO,"the taskno is is %s",ptaskno);
	
	pcompanyid=SPI_getvalue(rettuple,tupdesc,16);
        elog(INFO,"the companyid is is %s",pcompanyid);


        elog(INFO,"测试中文"); 
	/*
	sprintf(pszcmd,"python /home/harold/emsharold/server/src/send_sms.py %s %s %s %s %s",pName,pName,pName,pName,pName);
	pfree(pName);
	ret = system(pszcmd);
	elog(INFO,"the python return %d",ret);
	ret = system("date > /postgresql_tmp/mylog.txt");
	elog(INFO,"the datetime output is %d",ret);
	printf("this is from my trigger");
	*/
	/* connect to SPI manager */
	if ((ret = SPI_connect()) < 0)
		elog(ERROR, "trigf (fired %s): SPI_connect returned %d", when, ret);
	/* get number of rows in table */
	memset(pszcmd,0,256);
	sprintf(pszcmd,"SELECT name FROM company where id=%s",pcompanyid);
	ret = SPI_exec(pszcmd, 0);
	if (ret < 0)
		elog(ERROR, "trigf (fired %s): SPI_exec returned %d", when, ret);
	/* count(*) returns int8, so be careful to convert */
	pcompanyname = SPI_getvalue(SPI_tuptable->vals[0],SPI_tuptable->tupdesc,1);
	elog(INFO,"the company name is %s",pcompanyname);


	// Send the sms
	memset(pszcmd,0,256);
        sprintf(pszcmd,"python /home/harold/emsharold/server/src/send_sms.py %s %s %s %s %s %s %s %s",psn,psm,prn,prm,pstate,pnum,ptaskno,pcompanyname);
        ret = system(pszcmd);
        elog(INFO,"the python return %d",ret);

        pfree(psn);
        pfree(psm);
        pfree(prn);
        pfree(prm);
        pfree(pstate);
        pfree(pnum);
        pfree(ptaskno);
        pfree(pcompanyid);
	pfree(pcompanyname);

//	i = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0],
//	SPI_tuptable->tupdesc,1,&isnull));
//	elog (INFO, "trigf (fired %s): there are %d rows in ttest", when, i);
	SPI_finish();
		
	if (checknull)
	{
//		SPI_getbinval(rettuple, tupdesc, 1, &isnull);
//		if (isnull)
//			rettuple = NULL;
	}
	return PointerGetDatum(rettuple);
}
